var Ratings = Class.create();

Object.extend(Ratings, {
  validate: function(form) { 
    errors = ""
  
    inputs  = Form.getElements(form); 
    type    = form.select('.rateable_type')[0];
    title    = form.select('.rateable_name')[0];
    creator = form.select('.rateable_creator')[0];
    url     = form.select('.rateable_url')[0];
    rating  = form.select('.rateable_rating')[0];
    
    switch(type.value) {
      case "song":
        if (title.value == "")
          errors += "Please enter a title.<br/>";
        if (creator.value == "")
          errors += "Please enter a singer or band.<br/>";
        break;
      case "book":
        if (title.value == "")
          errors += "Please enter a title.<br/>";
        if (creator.value == "")
          errors += "Please enter an author.<br/>";
        break;
      case "film":
        if (title.value == "")
          errors += "Please enter a title.<br/>";
        break;
      case "website":
        if (url.value == "")
          errors += "Please enter a link.<br/>";
        break;
    }
    
    if (rating.value == "Rating") {
      errors += "Please enter a rating.<br/>";
    }
    
    if (errors == "")
      return true;
    else {
      Submit.updateErrors(errors);
      Ratings.resetForm(form, false);
      return false;
    }
  },
  
  changeType: function(el) {
    type = el.value;
    
    form = el.up('form');
    
    // Show all elements
    Form.getElements(form).each(Element.show);

    // Reset default dormant values.
    title   = form.select('.rateable_name')[0];
    creator = form.select('.rateable_creator')[0];
    url     = form.select('.rateable_url')[0];
    title.setAttribute('dormant_value', 'Title'); 
    creator.setAttribute('dormant_value', 'Creator'); 
    url.setAttribute('dormant_value', 'Link (Optional)'); 
    
    // Specialize
    switch(type) {
      case "song":
        creator.setAttribute('dormant_value', 'Singer / Band Name'); 
        break;
      case "book":
        creator.setAttribute('dormant_value', 'Author'); 
        break;
      case "film":
        creator.setAttribute('dormant_value', 'Director (Optional)'); 
        break;
      case "website":
        title.setAttribute('dormant_value', ''); 
        title.hide(); 
        creator.setAttribute('dormant_value', ''); 
        creator.hide(); 
        url.setAttribute('dormant_value', 'Link'); 
        break;
    }
    
    Ratings.resetForm(form, true);
  },
  
  resetForm: function(form, resetValues) {
    inputs = Form.getElements(form);
    
    // Reset all values and make dormant
    inputs.each(function(input) {
      if ((input.type == "text") && !input.hasClassName('submit')) {
        if (resetValues || (input.value == '')) {
          input.value = '';
          OnLoad.dormantAction.bind(input)();
        }
      }
      if (input.hasClassName("rateable_rating")) {
        input.value = "Rating";
      }
    });
  },
  
  updateLastRowClass: function() {
    rows = $('rateable_objects_list').select('tr');
    last_row = rows[rows.size() - 1];
    
    if (!last_row.hasClassName("odd") && !last_row.hasClassName("even")) {
      // See the row before last
      nextClassName = (rows[rows.size() - 2].hasClassName("even") ? "odd" : "even");
      
      last_row.addClassName(nextClassName);
    }
  },
  
  populateRating: function(ratings) {
    $$('select.rating_selection').each(function(el) {
      rateable_object_id = el.getAttribute("rateable_object_id");
      if ((rateable_object_id) && (ratings[rateable_object_id])) {
        el.value = ratings[rateable_object_id];
      }
    });
  },
  
  startLoad: function() {
    $('ratings_container_loading').show();
  },
  
  stopLoad: function() {
    $('ratings_container_loading').hide();
  }
});
